﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace EAppointmentsWPF
{
	public partial class MainWindowClass
	{
		public MainWindowClass()
		{
			this.InitializeComponent();
            InitializeItems();
            // Insert code required on object creation below this point.
        }
/*
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }*/

        protected override void OnInitialized(EventArgs e)
        {
            LoginControl.EnablingItems += new EventHandler(LoginSuccessful_EnablingItems);
            AppointmentListControl.AppointmentSelected+=new EventHandler(AppointmentListControl_AppointmentSelected);
            DateViewControl.monthCalenderDateChanged+=new EventHandler(DateViewControl_monthCalenderDateChanged);
            DetailsControl.WindowCollapsed+=new EventHandler(DetailsControl_WindowCollapsed);
            base.OnInitialized(e);
        }
        
        void InitializeItems()
        {
            LeftPanel.Visibility = Visibility.Hidden;
            CenterPanel.Visibility = Visibility.Hidden;
            RightPanel.Visibility = Visibility.Hidden;
            txtWelcome.Visibility = Visibility.Hidden;
            btnHelp.Visibility = Visibility.Hidden;
            btnLogout.Visibility = Visibility.Hidden;
        }
        void LoginSuccessful_EnablingItems(object sender, EventArgs e)
        {

            LeftPanel.Visibility = Visibility.Visible;
            CenterPanel.Visibility = Visibility.Visible;
            RightPanel.Visibility = Visibility.Visible;
            txtWelcome.Visibility = Visibility.Visible;
            btnHelp.Visibility = Visibility.Visible;
            btnLogout.Visibility = Visibility.Visible;
            txtUser.Text = "John Doe";
            ((Storyboard)this.Resources["Reappear"]).Begin(this);
            MainWindow.Focus();
            AppointmentListControl_AppointmentSelected(sender,e);
        }
        void AppointmentListControl_AppointmentSelected(object sender, EventArgs e)
        {
            PatientDetailsControl.txtName.Text = AppointmentListControl.obj.PatientName;
            string[] arr=new string[2];
            arr = AppointmentListControl.obj.PatientName.Split(' ');
            PatientDetailsControl.txtFirstName.Text = arr[0];
            PatientDetailsControl.txtLastName.Text = arr[1];
            PatientDetailsControl.txtUBRN.Text = AppointmentListControl.obj.UBRNno;
            PatientDetailsControl.txtDate.Text = AppointmentListControl.obj.Datevalue;
            PatientDetailsControl.txtProvider.Text = AppointmentListControl.obj.Provider;
            PatientDetailsControl.txtTime.Text = AppointmentListControl.obj.Time;
            PatientDetailsControl.txtReferrer.Text = AppointmentListControl.obj.ReferrerName + ", " + AppointmentListControl.obj.ReferrerAddress;
            PatientDetailsControl.txtStatus.Text = AppointmentListControl.obj.Statusvalue;
            PatientDetailsControl.txtEmail.Text = AppointmentListControl.obj.Email;
            PatientDetailsControl.txtAddress.Text = AppointmentListControl.obj.Address;
            PatientDetailsControl.txtPhoneNo.Text = AppointmentListControl.obj.PhoneNo.ToString();
            PatientDetailsControl.txtZipcode.Text = AppointmentListControl.obj.Zipcode.ToString();
        }

        void DateViewControl_monthCalenderDateChanged(object sender, EventArgs e)
        {
            if (DateViewControl.txtMonthCalenderDate.Text != "" && DateViewControl.txtMonthCalenderDate.Text != "TextBox")
            {
                TimelineWeekControl.txtBox.Text = DateViewControl.txtMonthCalenderDate.Text.Substring(0, 10);
                TimelineDayControl.txtBox.Text = DateViewControl.txtMonthCalenderDate.Text.Substring(0, 10);
            }
        }

        void DetailsControl_WindowCollapsed(object sender, EventArgs e)
        {
            DetailsControlGrid.Visibility = Visibility.Hidden;
        }
        private void btnNewAppt_Click(object sender, RoutedEventArgs e)
        {

            DetailsControlGrid.Visibility = Visibility.Visible;
            DetailsControl.Visibility = Visibility.Visible;
            ((Storyboard)DetailsControl.Resources["ShowAppointmentWindow"]).Begin(DetailsControl);
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            LeftPanel.Visibility = Visibility.Hidden;
            CenterPanel.Visibility = Visibility.Hidden;
            RightPanel.Visibility = Visibility.Hidden;
            txtWelcome.Visibility = Visibility.Hidden;
            btnHelp.Visibility = Visibility.Hidden;
            btnLogout.Visibility = Visibility.Hidden;
            DetailsControlGrid.Visibility = Visibility.Hidden;
            LoginControl.Visibility = Visibility.Visible;
            LoginControl.txtPassword.Clear();
            LoginControl.txtUsername.Clear();
            LoginControl.txtUsername.Focus();
            txtUser.Text = "";
            //this.Close();
        }

        private void ViewTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ViewTabs.SelectedIndex == 0)
            {
                DateViewControl.DateCalender.Visibility = Visibility.Visible;
                DateViewControl.MonthCalender.Visibility = Visibility.Hidden;
            }
            else if (ViewTabs.SelectedIndex == 1)
            {
                DateViewControl.MonthCalender.Visibility = Visibility.Visible;
                DateViewControl.DateCalender.Visibility = Visibility.Hidden;
                TimelineDayControl.ScrollViewer.ScrollToVerticalOffset(212);
            }
            else
            {
                DateViewControl.DateCalender.Visibility = Visibility.Hidden;
                DateViewControl.MonthCalender.Visibility = Visibility.Visible;
                TimelineWeekControl.ScrollViewer.ScrollToVerticalOffset(212);
            }
        }
    }

}